package cn.sinata.xldutil_kotlin.net

import com.tanguy.user.Const
import com.tanguy.user.net.http.json.JsonConverterFactory
import com.tanguy.user.utlis.extend.loge
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory

/**
 *
 */
class RRetrofit private constructor() {

    val retrofit by lazy {
        val builder = Retrofit.Builder()
                .baseUrl(Const.Api.BASE_URL)//注意此处,设置服务器的地址
                .addConverterFactory(JsonConverterFactory.create())//用于Json数据的转换,非必须
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//用于返回Rxjava调用,非必须
        if (Const.DEBUG.REQUEST) {
            builder.client(OkHttpClient.Builder()
                    .addNetworkInterceptor { chain ->
                        val request = chain.request()
                        val proceed = chain.proceed(request)
                        loge(request?.url().toString())
                        proceed!!
                    }.build())
        }
        builder.build()
    }
    val retrofit_gaode: Retrofit = Retrofit.Builder()
            .baseUrl(Const.Api.GAODE_URL)//注意此处,设置服务器的地址
            .addConverterFactory(JsonConverterFactory.create())//用于Json数据的转换,非必须
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//用于返回Rxjava调用,非必须
            .build()

    companion object {
        fun instance(): RRetrofit = INSTANCE.instance
    }

    private object INSTANCE {
        val instance = RRetrofit()
    }

    fun <T> create(clazz: Class<T>): T {
        return retrofit.create(clazz)
    }

    fun <T> create_gaode(clazz: Class<T>): T {
        return retrofit_gaode.create(clazz)
    }
}